package com.zb.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zb.Service.PowerService;
import com.zb.Service.RecordService;
import com.zb.entity.Power;
import com.zb.entity.User;

@Scope("prototype")
@Component
@RequestMapping("/power")
public class PowerController {
	@Resource
	private PowerService ps;
	@Resource
	private RecordService rs;

	@RequestMapping(params = "op=show")
	public String toShow(ModelMap modelMap) {
		List<Power> plist = ps.findPowerAll();
		modelMap.put("plist", plist);
		return "jurisdiction";
	}

	@RequestMapping(params = "op=showid")
	public String toShowid(int id, ModelMap modelMap) {
		Power p = ps.getPowerByid(id);
		modelMap.put("pb", p);
		List<Power> plist = ps.findPowerAll();
		modelMap.put("plist", plist);
		return "jurisdiction";
	}
	
	@RequestMapping(params = "op=update")
	public String toUpdate(int id, Integer sysconfig, Integer loginrecord,
			Integer actionrecord, Integer rolemanager, Integer powermanager, Integer showinfo,
			Integer showtask, Integer showtip, Integer shownote, ModelMap modelMap,HttpSession session) {
		Power p = ps.getPowerByid(id);
		if(sysconfig==null){
			p.setSysconfig(0);
		}else{
			p.setSysconfig(1);
		}
		if(loginrecord==null){
			p.setLoginrecord(0);
		}else{
			p.setLoginrecord(1);
		}
		if(actionrecord==null){
			p.setActionrecord(0);
		}else{
			p.setActionrecord(1);
		}
		if(rolemanager==null){
			p.setRolemanager(0);
		}else{
			p.setRolemanager(1);
		}
		if(powermanager==null){
			p.setPowermanager(0);
		}else{
			p.setPowermanager(1);
		}
		if(showinfo==null){
			p.setShowinfo(0);
		}else{
			p.setShowinfo(1);
		}
		if(showtask==null){
			p.setShowtask(0);
		}else{
			p.setShowtask(1);
		}
		if(showtip==null){
			p.setShowtip(0);
		}else{
			p.setShowtip(1);
		}
		if(shownote==null){
			p.setShownote(0);
		}else{
			p.setShownote(1);
		}
		ps.updatePower(p);
		List<Power> plist = ps.findPowerAll();
		modelMap.put("plist", plist);
		User user = (User) session.getAttribute("sys_user");
		rs.addRecord(user.getId(), "修改了用户权限");
		return "jurisdiction";
	}

}
